<?php
namespace Addons\Wechat\Controller;
class FeedsController extends \Addon {
	public function __construct() {
		parent::__construct();
		$this->feeds_type = BoxModel('addon_wx_image_type');
		$this->feeds = BoxModel('addon_wx_image_text');
		$this->id = I('id', '', 'intval');
	}

	/**
	 * [index 推送管理首页]
	 *
	 */
	public function index() {
		$where = "store_id='$this->admin_store_id'";
		$kw = I('get.kw', '', 'trim');
		if ($kw) {
			$where .= ' and `name` like "%' . $kw . '%" or `keyName` like "%' . $kw . '%"';
		}
		/*****数据初始化*******/
		$condition = "keyName='firstConcern' and store_id='$this->admin_store_id'";
		$r = $this->feeds_type->where($condition)->find();
		if (!$r) {
			$row = array(
				'keyName' => 'firstConcern',
				'store_id' => $this->admin_store_id,
				'name' => '首次关注',
				'text' => '欢迎关注我们啊啊！！！！',
			);
			$this->feeds_type->add($row);
		}
		/*********到这里*******/
		$order = 'id desc';
		$this->setPager($this->feeds_type, 12, $where);
		$this->data = $this->feeds_type->where($where)->order($order)->limit($this->limit)->select();
		if (I('select')) {
			$this->admin_display("index_select");
		} else {
			$this->admin_display("index");
		}

	}

	public function addType() {
		if ($this->id) {
			$this->data = $this->feeds_type->where(array('id' => $this->id))->find();

		}
		$this->admin_display();
	}

	public function checkKey() {
		$find = $this->feeds_type->where(array('keyName' => I('keyName')))->find();
		if ($find) {
			echo 'false';
		} else {
			echo 'true';
		}
	}

	public function insertType() {
		$keyName = I('keyName');
		S($keyName, NULL);
		if (IS_POST) {
			$data = array(
				'name' => I('name'),
				'keyName' => I('keyName'),
				'store_id' => $this->admin_store_id,
				'text' => $_POST['text'],
			);
			if (!$this->id) {
				$this->feeds_type->add($data);
				$this->jump("操作成功");
			} else {
				$this->feeds_type->where(array('id' => $this->id, 'store_id' => $this->admin_store_id))->save($data);
				$this->jump("操作成功");
			}
		}
	}

	/**
	 * 删除分类
	 * @return [type]
	 */
	public function deleteType() {
		$keyName = I('keyName');
		S($keyName, NULL);
		$this->feeds_type->where(array('id' => $this->id, 'store_id' => $this->admin_store_id))->delete();
		$this->jump("操作成功");

	}

	public function feedslist() {
		//  $this->memuNav="index";
		$keyName = I('keyName');
		$where = "store_id='$this->admin_store_id'";
		$order = 'id desc';
		$where .= " and keyName='$keyName'";
		$this->feeds_data = $this->feeds_type->where("keyName='$keyName'")->find();
		$this->data = $this->feeds->where($where)->order($order)->select();
		$this->keyName = $keyName;
		$this->admin_display();
	}

	/**
	 *  添加推送
	 */
	public function addFeeds() {
		//  $this->memuNav="index";
		// dump($this->feeds);
		$keyName = I('keyName');
		$this->feeds_data = $this->feeds_type->where("keyName='$keyName'")->find();
		if ($this->id) {
			$this->data = $this->feeds->where(array('id' => $this->id))->find();
		}
		$this->admin_display();
	}

	public function feedsInsert() {
		$keyName = I('keyName');
		S($keyName, NULL);
		if (IS_POST) {
			if (empty($this->id)) {

				$info = $this->_upload("/feeds/");
				$data['keyName'] = I('keyName');
				$data['url'] = urlSet(I('url'));
				$data['tip'] = I('tip');
				if (is_array($info)) {
					$data['img'] = $info['img']['savepath'] . $info['img']['savename'];
				}

				$data['store_id'] = $this->admin_store_id;
				$data['title'] = I('title');
				$this->feeds->add($data);
				$this->redirect(addons_url('feedslist', array('keyName' => $data['keyName'], 'memu' => 'System')));
			} else {
				$info = $this->_upload("/feeds/");
				if ($info == false) {
					$arr['img'] = I('oldimg');
				} else {
					$arr['img'] = $info['img']['savepath'] . $info['img']['savename'];
				}
				$arr['url'] = (I('url'));
				$arr['tip'] = I('tip');
				$keyName = I('keyName');
				$arr['title'] = I('title');
				$this->feeds->where(array('id' => $this->id, 'store_id' => $this->admin_store_id))->save($arr);
				$this->redirect(addons_url('feedslist', array('keyName' => $keyName, 'memu' => 'System')));
			}
		}
	}

	/**
	 * delete 删除
	 * @return [type]
	 */
	public function deleteFeeds() {
		$keyName = I('keyName');
		S($keyName, NULL);
		$this->feeds->where(array('id' => $this->id, 'store_id' => $this->admin_store_id))->delete();
		$this->jump("删除成功");
		//$this->redirect(U('feedslist',array('keyName'=>I('keyName'))));
	}

//显示微信二维码
	function show_wx_scan() {
		$id = I('id');
		$this->param = BoxModel("addon_wx_config")->where(array('store_id' => $this->admin_store_id))->find();
		$wechatObj = new \Common\Org\weiXin($this->param);
		$qrcodeUrl = $wechatObj->qrcode($id);
		echo '<img class="qrcode lightBorder" src="' . $qrcodeUrl . '">';
	}
	//发送素材
	function sendMaterial() {
		$DosendMaterial = S("DosendMaterial_" . $this->admin_store_id);
		if ($DosendMaterial) {
			$this->jump("群发还没有完成", addons_url('getSendMsg'));
		}
		$this->openids = I('openids') ? I('openids') : array();
		S("sendMaterialOpenids_" . $this->admin_store_id, $this->openids);
		$this->admin_display();
	}

	function doSendMaterial() {
		ignore_user_abort();
		$sendMaterialOpenids = S("sendMaterialOpenids_" . $this->admin_store_id);
		$this->storeInfo = BoxModel("addon_wx_config")->load($this->admin_store_id);
		$keyName = urldecode(I('keyName'));
		if (!$keyName) {
			$this->jump("请先选择素材");
		}
		if (empty($sendMaterialOpenids)) {
			$this->jump("请先选择用户");
		}
		//dump($this->storeInfo);
		$param = array(
			'token' => $this->storeInfo['token'],
			'appID' => $this->storeInfo['appID'],
			'appsecret' => $this->storeInfo['appsecret'],
			'store_id' => $this->store_id,
		);
		S("DosendMaterial_" . $this->admin_store_id, 1);
		$wechatObj = new \Common\Org\weiXin($param);
		$sended = array();
		foreach ($sendMaterialOpenids as $openid) {
			$r = $wechatObj->sendMaterial($keyName, $openid);
			$sended[$openid] = $r;
			S("sended_" . $this->admin_store_id, $sended, 3600);
		}
		dump($sended);
		S("DosendMaterial_" . $this->admin_store_id, null);
		S("sendMaterialOpenids_" . $this->admin_store_id, null);
	}

	function getSendMsg() {
		$sendMaterialOpenids = S("sendMaterialOpenids_" . $this->admin_store_id);
		$sended = S("sended_" . $this->admin_store_id);
		if (count($sended) == count($sendMaterialOpenids)) {
			echo 1;
		} else {
			echo count($sended) . "/" . count($sendMaterialOpenids);
		}

	}

}

?>